/*
   CC.NET SideBar Gadget for Windows Vista 
   (C) Simone Chiaretta, 2007
   http://codeclimber.net.nz

   This project is licensed under the new BSD license.
   See the License.txt file for more information.
*/

var AjaxProxy = function()
{
}

AjaxProxy.prototype = 
{
   _handler: null,
   _errorHandler: null,
   _xmlHttp: null,
   _currentUrl: null,
   InternalAjaxHandler: function()
   {
      var projectArray = new Array();
      if(_xmlHttp.readyState==4)
      {
         if(_xmlHttp.status==200)
         {
            var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async="false";
            xmlDoc.loadXML(_xmlHttp.responseText);
            
            var projects = xmlDoc.documentElement.childNodes;
            
            for (var iNode = 0; iNode < projects.length; iNode++)
            {
               var projectContent=projects.item(iNode);
               var currentProject = new Project(_currentUrl);
               currentProject.ReadFromXmlNode(projectContent);
               projectArray[iNode] = currentProject;
            }
            
            if( _handler != null )
            {
               _handler( projectArray );
            }
         }
         else
         {
            if( _errorHandler != null )
            {
               _errorHandler( _xmlHttp.statusText );
            }
         }
      }
   },
   RequestXmlStatus: function(url, positiveCallback, errorHandler)
   {
      //send envelope
      _xmlHttp = new XMLHttpRequest();
      _handler = positiveCallback;
      _errorHandler = errorHandler;
      _xmlHttp.onreadystatechange = this.InternalAjaxHandler;
      _currentUrl = url;
      
      finalUrl=this.buildUrl(url);
      
      _xmlHttp.open("GET",finalUrl,true);
      _xmlHttp.send(null);
   },
   buildUrl: function(url)
	{
	   if(url.charAt(url.length-1)!='/')
		   url = url+'/';

      url = url + "XmlStatusReport.aspx";

      var now = new Date();
      finalUrl = url + "?sid=" + now.getTime();
		      
	   return finalUrl;
	}
}